package threads;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;

import data.CustomerInfo;
import data.ItemList;
import rm2.DRSserverinit;
//import exception.ShowIsFullException;

public class ItemAddition 
{
	int number;
	CustomerInfo cl;
	DRSserverinit drSserverinit;
	public ItemAddition(DRSserverinit drSserverinit)
	{		
		this.drSserverinit=drSserverinit;
	}
	
	
	public void addItem(String customer_id, String item_id, int number) throws IOException
	{
		File directory = new File(this.drSserverinit.input_folder_path + File.separator + "CustomerFile" /*+ File.separator*/);  
		System.out.println("directory is "+directory.getAbsolutePath().toString());
		File[] customer_files = directory.listFiles(); 
		String[] token_list, item_list; 
		int count = 0;
		String customer="", itemInfo="", itemUpdate="";
		int numberOfItems=0;
		boolean newItemFlag = true;
		ItemList il;
		

		
		
		for (int index = 0; index < customer_files.length; index++)  
		{
			if (customer_files[index].exists())
			{
				//System.out.println("test1");
				if(!customer_files[index].isDirectory()){
					
				
				BufferedReader br = new BufferedReader(new FileReader(customer_files[index]));
				String line;
				int ind = 0, line_count=1;
				
				while ( (line = br.readLine()) != null)
				{
					
					if(customer_files[index].getName().toString().equalsIgnoreCase(customer_id))
					{						
						token_list = line.split(":");
	
							if (line_count == 1)
							{
								//read the customer ID
								customer = token_list[1].trim();
								//System.out.println(customer);
							}
							
							if(line_count == 2)
							{
								// Read the item information								
								itemInfo =  token_list[1].trim();
								item_list = itemInfo.split(",");
								
								for(int i = 0; i<item_list.length; i++)
								{
									if(item_list[i].contains(item_id))
									{
										
										numberOfItems = Integer.parseInt(item_list[i].replace(item_id, "").trim());
										numberOfItems = numberOfItems + number;										
										item_list[i] = item_id + " " + Integer.toString(numberOfItems);									
										
										// no update 
										newItemFlag = false;										
									}
									
									if (itemUpdate.equals(""))
									{
										itemUpdate = item_list[i];
									}
									else
									{
										itemUpdate = itemUpdate + "," + item_list[i];
									}									
								}
								
								if(newItemFlag == true)
								{
									if (itemUpdate.equals(""))
									{
										itemUpdate = item_id + " " + Integer.toString(number);
									}
									else
									{
										itemUpdate = itemUpdate + "," + item_id + " " + Integer.toString(number);
									}									
								}
								
								//updating the hash map by remove and add the updated one
								il = new ItemList(item_id, drSserverinit.slist.get(item_id).getCapacity()- number,drSserverinit);
								drSserverinit.slist.put(item_id, il);								
								
								//System.out.print("\nUpdated " + customer_id + ": " + itemUpdate + ". ");
							}
							line_count++;							
						}				
				}
				br.close();
			}
			}
			
		}
		
		
		//Writing the itemList file
	    FileWriter fs = new FileWriter(this.drSserverinit.input_folder_path + File.separator + "ItemList");
    	BufferedWriter bw = new BufferedWriter(fs);
	    //out.write("Customer ID:" + customer_id);
    	Iterator it = drSserverinit.slist.keySet().iterator(); 
    	
    	while(it.hasNext()) 
    	{ 
    		String key = (String) it.next(); 
    		int val = drSserverinit.slist.get(key).getCapacity();
    		
    		bw.write(key);
    		bw.write(" ");
    		bw.write(Integer.toString(drSserverinit.slist.get(key).getCapacity()));    		
    		bw.newLine();
    	}
    	
    	bw.close();
    	fs.close();
		
		
		
		//end of file listing For loop

			
		
		
		    // Create file 
		    FileWriter fstream = new FileWriter(this.drSserverinit.input_folder_path + File.separator + "CustomerFile" + File.separator + customer_id);
	    	BufferedWriter out = new BufferedWriter(fstream);
		    out.write("Customer ID:" + customer_id);
		    out.newLine();
		    
		    out.write("Item:" + itemUpdate);		    
		    
		    		    
		    //Close the output stream
		    out.close();
		    fstream.close();
		    //System.out.println("Non-co: " + sl.getCourseReg().toString());
		    //System.out.println("test2");
		    //System.out.println(cl.getCustomerID()  +" --> Bought" + ": " + "Customer boutght " + item_id);
		    //System.out.println("test3");	    

	}

}
